package com.hrzyit.whistle.cases.data.repository

import com.hrzyit.base.data.net.RetrofitFactory
import com.hrzyit.base.data.protocol.BaseResp
import com.hrzyit.whistle.cases.data.api.UploadApi
import com.hrzyit.whistle.cases.data.bean.UploadInfo
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
// import rx.Observable
import io.reactivex.Observable
import java.io.File
import javax.inject.Inject

/**
 * 文件上传数据操作类
 * @author YangWei
 * @date 2019/3/31
 */
class UploadRepository @Inject constructor() {

    /**
     * 上传图片
     */
    fun uploadPhoto(photo: String): Observable<BaseResp<UploadInfo>> {

        val uploadFile = File(photo)

        val builder: MultipartBody.Builder = MultipartBody.Builder()

        // 构建上传文件 RequestBody
        val fileBody = RequestBody.create(
            MediaType.parse("multipart/form-data"), uploadFile
        )

        builder.addFormDataPart("file", uploadFile.name, fileBody)

        val parts = builder.build().parts()

        return RetrofitFactory.instance
            .create(UploadApi::class.java).upload(
                parts
            )
    }


    /**
     * 上传音频文件
     * @author YangWei
     * @date 2019/4/13
     * @param audioPath 本地音频文件路径
     * @return Observable<UploadInfo>   FastDfs 文件路径对象 {@link UploadInfo}
     */
    fun uploadRecordAudio(audioPath: String): Observable<BaseResp<UploadInfo>> {

        val uploadFile = File(audioPath)

        val builder: MultipartBody.Builder = MultipartBody.Builder()

        // 构建上传文件 RequestBody
        val fileBody = RequestBody.create(
            MediaType.parse("multipart/form-data"), uploadFile
        )

        builder.addFormDataPart("file", uploadFile.name, fileBody)

        val parts = builder.build().parts()

        return RetrofitFactory.instance
            .create(UploadApi::class.java).uploadRecordAudio(
                parts
            )
    }


}